package net.zjitc.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by Administrator on 2017/9/27.
 */
@WebServlet(name = "HttpServlet",urlPatterns = "/countServlet")
public class HttpServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //统计访问，放在哪里
        ServletContext context=this.getServletContext();
        response.setContentType("text/html;charset=utf-8");
        //context里面设置一个属性count--->>n
        PrintWriter out=response.getWriter();

        Integer count;
        count=(int)context.getAttribute("count");

        if (count == null){
            count=1;
            out.write("这是第"+count+"次访问");
        }else {
            count++;
            out.write("这是第"+count+"次访问");

        }
        out.write("hahhahahahhaa");
        //保存到context,下次才能取到+1的值
        context.setAttribute("count",count);
    }
}
